/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import PDFpacket.Pdf_RicevutaP;
import Utils.Product;
import Utils.Sell;
import Utils.User;
import Utils.dbManager;
import com.itextpdf.text.DocumentException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alice
 */
public class BuyerAscoltaConferma extends HttpServlet {
     private dbManager manager;
      int index=0;
   
    @Override
public void init() throws ServletException {
// inizializza il DBManager dagli attributi di Application
this.manager = (dbManager)super.getServletContext().getAttribute("dbmanager");
}
   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, FileNotFoundException {
        try{
             HttpSession session = request.getSession(false);
        if(session.getAttribute("buyer")==null)
            response.sendRedirect(request.getContextPath() + "/ErrorLoginServlet");
        
             String idProdotto=(String)session.getAttribute("idProdotto");
             int idp=Integer.parseInt(idProdotto);
             
             
             int cont=0;
              if (session == null) {
                 response.sendRedirect(request.getContextPath()+ "/ErrorLoginServlet");
             } else {
    
    
       User user=(User) session.getAttribute("buyer");
                  
        List<Product> product = manager.getConfirm(idp);
         for (int i = 0; i < product.size(); i++) {
    idp = product.get(i).getId_Product();
    String pr= product.get(i).getProduct();
   float f= product.get(i).getPrice();
   String u= product.get(i).getUm();
   String c= product.get(i).getCategory();
   String id= product.get(i).getUsername();
   int q= product.get(i).getQuantity();
    
int idBuyer=user.getId_User();
String nameBuyer=user.getUsername();

  
       ServletContext ctx=getServletContext();
        String contextPath=ctx.getRealPath(File.separator);
        //System.out.println("CTX= "+ctx+" CP AC= "+contextPath);
        String pdfFileName=idBuyer+"_BuyerRicevutaAcquistoIPD_"+idp+".pdf";
        
        
        File pdfFile=new File(contextPath+"PDF/"+nameBuyer+"/"+pdfFileName);
        
        Pdf_RicevutaP pdf = new Pdf_RicevutaP();
      
        pdf.Genera(pr,f,u,c,id,q,request,response,pdfFileName,nameBuyer);
       
         
        
        //TABELLA SELL
        manager.upAcquisto(idp,id,idBuyer,q,pdfFileName,f);
       
        
        manager.getBuy(idp);
       
        String mex ="Prodotto Acquistato! In allegato il PDF!";
        session.setAttribute("confirm2", mex);

     
       response.sendRedirect(request.getContextPath()+ "/BuyerLandingServlet");}
        
      
        }} catch (DocumentException ex) {
            Logger.getLogger(BuyerAscoltaConferma.class.getName()).log(Level.SEVERE, null, ex);
        }catch (SQLException ex) {
throw new ServletException(ex);
} 
    }

}